From 6693b9c8ff1ae1ec02c9002c0a8f5f416f0c88f0 Mon Sep 17 00:00:00 2001
From: Maxim Cournoyer <maxim.cournoyer@gmail.com>
Date: Sun, 12 Jan 2020 23:23:33 -0500
Subject: [PATCH 3/3] build: Prefer googletest from the system.

Fixes issue #4.

The build system now look for a system installed googletest or falls
back to the bundled copy of googletest otherwise.

* CMakeLists.txt: Add call to find_package(GTest).
(include_directories): Remove "src/googletest/googletest/include" from
arg.
* src/CMakeLists.txt[NOT GTEST_FOUND]: Define the gtest and gtest_main
libraries when GTEST_FOUND is false.  Globally include the googletest
headers here.  Define aliases for the gtest and gtest_main libraries
that match those defined by the FindGTest module.
* src/tests/CMakeLists.txt: Replace references to gtest_main and gtest
by GTest::Main and GTest::GTest, respectively.
---
 CMakeLists.txt           |  3 ++-
 src/CMakeLists.txt       | 15 +++++++++++----
 src/tests/CMakeLists.txt |  6 +++---
 3 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index bfeb8f03..96fbd58c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -25,13 +25,14 @@ find_package(DoubleConversion REQUIRED)
 find_package(PkgConfig REQUIRED)
 find_package(Cython)
 find_package(Threads)
+find_package(GTest)
 pkg_check_modules(GTK3 gtk+-3.0)
 pkg_check_modules(GLIB glib-2.0)
 pkg_check_modules(CAIRO cairo)
 pkg_check_modules(GSL gsl)
 
 # Add global include and link directories
-include_directories(src src/googletest/googletest/include ${CMAKE_CURRENT_BINARY_DIR})
+include_directories(src ${CMAKE_CURRENT_BINARY_DIR})
 link_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/2geom)
 
 check_cxx_source_compiles("#include <math.h>\nint main() { double a=0.5,b=0.5,c=0.5; sincos(a, &b, &c); return 0; }" HAVE_SINCOS)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index eefb3ac5..941dc4c7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,7 +1,14 @@
-add_library(gtest SHARED googletest/googletest/src/gtest-all.cc)
-target_include_directories(gtest PRIVATE googletest/googletest)
-target_link_libraries(gtest Threads::Threads)
-add_library(gtest_main STATIC googletest/googletest/src/gtest_main.cc)
+if(NOT GTEST_FOUND)
+  message("No system googletest library: using bundled copy.")
+  add_library(gtest SHARED googletest/googletest/src/gtest-all.cc)
+  add_library(gtest_main STATIC googletest/googletest/src/gtest_main.cc)
+  target_include_directories(gtest PRIVATE googletest/googletest)
+  target_link_libraries(gtest Threads::Threads)
+  include_directories(SYSTEM googletest/googletest/include)
+  # Aliases to share the same nomenclature with FindGTest.
+  add_library(GTest::GTest ALIAS gtest)
+  add_library(GTest::Main ALIAS gtest_main)
+endif()
 
 add_subdirectory(2geom)
 add_subdirectory(tests)
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index 626cfd87..3538f8cf 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -34,15 +34,15 @@ foreach(source ${2GEOM_GTESTS_SRC})
 	add_executable(${source} ${source}.cpp)
 	target_include_directories(${source} PRIVATE ${GSL_INCLUDE_DIRS}
 	  ${GTK3_INCLUDE_DIRS} ${GLIB_INCLUDE_DIRS})
-	target_link_libraries(${source} 2geom gtest_main gtest ${GSL_LIBRARIES}
-	  ${GTK3_LIBRARIES} ${GLIB_LIBRARIES})
+	target_link_libraries(${source} 2geom GTest::Main GTest::GTest
+	  ${GSL_LIBRARIES} ${GTK3_LIBRARIES} ${GLIB_LIBRARIES})
 	add_test(NAME ${source} COMMAND ${source})
 endforeach()
 
 foreach(source ${2GEOM_TESTS_SRC})
     add_executable(${source} ${source}.cpp)
     target_include_directories(${source} PRIVATE ${GSL_INCLUDE_DIRS} ${GTK3_INCLUDE_DIRS})
-    target_link_libraries(${source} 2geom gtest ${GSL_LIBRARIES} ${GTK3_LIBRARIES})
+    target_link_libraries(${source} 2geom GTest::GTest ${GSL_LIBRARIES} ${GTK3_LIBRARIES})
     add_test(NAME ${source} COMMAND ${source})
 endforeach(source)
 
-- 
2.24.1

